﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage.adminUser
{
    public partial class Modify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                int id = int.Parse(Request["id"]);
                AdminRoleBLL urb = new AdminRoleBLL();
                this.ddl.DataSource = urb.GetAllAdminRole();
                this.ddl.DataTextField = "roleName";
                this.ddl.DataValueField = "id";
                this.ddl.DataBind();
                AdminUserBLL aub = new AdminUserBLL();
                AdminUser user = aub.GetAdminUserByID(id);
                this.txtUserName.Text = user.userName;
                foreach (ListItem it in ddl.Items)
                {
                    if (it.Value == user.roleID.ToString())
                    {
                        it.Selected = true;
                        break;
                    }
                }
                this.hfID.Value = user.id.ToString();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userName = StringUtil.checkStr(this.txtUserName.Text);
            string password = StringUtil.checkStr(this.txtPassword.Text);
            int id = int.Parse(this.hfID.Value);
            int roleID = int.Parse(this.ddl.SelectedValue);
            AdminUserBLL aub = new AdminUserBLL();
            AdminUser user = aub.GetAdminUserByID(id);
            user.userName = userName;
            if (!string.IsNullOrEmpty(password))
            {
                user.password =StringUtil.Md5(password);
            }
            user.roleID = roleID;
            aub.ModifyAdminUser(user);

            AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
            ArtTypeBLL atbll = new ArtTypeBLL();
            OperationLogBLL olbll = new OperationLogBLL();
            OperationLog ol = new OperationLog();
            ol.AddDate = DateTime.Now;
            ol.Ip = StringUtil.GetIP();
            ol.ModuleName = "修改管理员角色";
            ol.Contents = "【修改】" + user.userName;
            ol.UserName = ali.UserName;
            olbll.Add(ol);

            StringUtil.Alert("修改成功","AddUser.aspx");


        }

    }
}
